//
//  WQHomeCell.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/10.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

let homeCellSubViewsMargin: CGFloat = 12
let userIconViewWith: CGFloat = 35

class WQHomeCell: UITableViewCell {

    var statusViewModel: WQStatusViewModel? {
        didSet {
            // TODO - 给子控件赋值
            statusUserInfoView.statusViewModel = statusViewModel
            contentLabel.text = statusViewModel?.statusContent
            pictureView.statusViewModel = statusViewModel
            
            //设置数据后更新pictureView的宽高
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
                
            }
            
        }
    }
    
    
    
    //
    func rowHeight(viewModel: WQStatusViewModel) -> CGFloat {
        //给自己的statusViewModel传值能对所有控件赋值
        statusViewModel = viewModel
        
//        print(CGRectGetMaxY(statusTabbar.frame))
        //强制计算所有的子控件位置
        contentView.layoutIfNeeded()
//        print(CGRectGetMaxY(statusTabbar.frame))
        //返回最大y值
        return CGRectGetMaxY(statusTabbar.frame)
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //取消tableViewCell的选种样式 -> 没有样式
        //注意不能设置tableView的allowSelection样式，这个禁止选中，tableView的cell就不能接收事件
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var separator = UIView()
    private lazy var statusUserInfoView: WQStatusUserInfoView = WQStatusUserInfoView()
    lazy var statusTabbar: WQStatusTabbar = WQStatusTabbar()
    lazy var pictureView: WQPictureView = WQPictureView()
    /// 微博正文标签
    lazy var contentLabel: UILabel = UILabel(title: "微博正文")

}


extension WQHomeCell {
    
    func setupUI() {
        self.contentView.addSubview(separator)
        separator.backgroundColor = UIColor.darkGrayColor()
        self.contentView.addSubview(statusUserInfoView)
        self.contentView.addSubview(contentLabel)
        contentLabel.textAlignment = NSTextAlignment.Left
        self.addSubview(pictureView)
        self.contentView.addSubview(statusTabbar)
 
        
        separator.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.contentView.snp_left)
            make.right.equalTo(self.contentView.snp_right)
            make.top.equalTo(self.contentView.snp_top)
            make.height.equalTo(8)
        }
        
        statusUserInfoView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.contentView.snp_left)
            make.top.equalTo(self.separator.snp_bottom)
            make.right.equalTo(self.contentView.snp_right)
            make.height.equalTo(homeCellSubViewsMargin + userIconViewWith)
        }
        
        
        // 2> 内容标签
        // MARK: - 必须调用preferredMaxLayoutWidth，不然高度计算不准确
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * homeCellSubViewsMargin
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusUserInfoView.snp_bottom).offset(homeCellSubViewsMargin)
            make.left.equalTo(contentView.snp_left).offset(homeCellSubViewsMargin)
            make.right.equalTo(contentView.snp_right).offset(-homeCellSubViewsMargin)
        }

//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(self.contentLabel.snp_bottom).offset(homeCellSubViewsMargin)
//            make.left.equalTo(self.contentLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(120)
//        }
        
        statusTabbar.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.pictureView.snp_bottom).offset(homeCellSubViewsMargin)
            make.left.equalTo(self.contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
    }
}